<?php
/*****************************************************
 * Project: PPDAO
 * 
 * 
 * Author: Ivan Femia
 * Last modify by: Ivan Femia
 * 
 ***************************************************** 
 * 
 * Abstract Action to be extended in your Custom 
 * Action
 * 
 *****************************************************/
abstract class CommonAction {
	
	protected $_param;
	protected $_errors;
	protected $_response;
	protected $_objectsList;
	
	const _SUCCESS 		= TRUE;
	const _FAIL 		= FALSE;
	
	public function __construct() {
		$this->_param 		= new Parameters();
		$this->_errors 		= new Errors();
		$this->_objectsList 	= new ObjectsList();
	}	
	
	/**
	 * Standard request method
	 *
	 * @param unknown_type $param
	 */
	public function __request(Parameters $_param) {
		if ( !is_array($_param) && !$_param instanceof Parameters ) {
			throw new IllegalArgument(); 
		}
		
		$this->_param 	= $_param;
	}
	
	//TODO: response due the action performed
	public abstract function __response();
	
	//TODO: Business logic to be implemented in derived class
	public abstract function __execute();
	
	public function getParameter ( $parName ) {
		return $this->_param->offsetGet("par".$parName);
	}

	public function getObjectList ( $objName ) {
		return $this->_objectsList->offsetGet($objName);
	}	
	
	public function appendError ( Error $_error ) {
		$this->_errors->append($_error);
	}
	
	public function appendObjectList ( ObjectList $_objectList ) {
		$this->_objectsList->offsetSet($_objectList->getName(),$_objectList);
	}
	
	public function getErrors ( ) {
		return $this->_errors;
	}
	
	public function getParameters ( ) {
		return $this->_param;
	}
	
	public function getObjectsList() {
		return $this->_objectsList;
	}
	
	/**
	 * @return unknown
	 */
	public function getResponse() {
		return $this->_response;
	}
	
	/**
	 * @param unknown_type $_response
	 */
	public function setResponse($_response) {
		$this->_response = $_response;
	}

	public function setObjectsList(ObjectsList $_objectsList) {
		$this->_objectsList = $_objectsList;
	}
	
	/**
	 * Enter description here...
	 *
	 * @param integer $_type a value between 1 and 7
	 * @return unknown
	 */
	public function countErrors ( $_type = Error::_ERROR ) {
		// if $_type value is not valid
		if ( $_type > 7 ) $_type = 7;
		if ( $_type < 0 ) $_type = 0;
		
		// start to count
		$counter = 0;
		$iterator = $this->getErrors()->getIterator();
		while($iterator->valid()) {
		    $error = $iterator->current();
		    switch ( $error->getType() ) {
		    	case Error::_INFO:
		    		if ( in_array( $_type, array(1,3,5,7) ) ) $counter++;
		    		break;
		    	case Error::_WARNING:
		    		if ( in_array( $_type, array(2,3,6,7) ) ) $counter++;
		    		break;
		    	case Error::_ERROR:
		    		if ( in_array( $_type, array(4,5,6,7) ) ) $counter++;
		    		break;		    		
		    }
		    $iterator->next();
		}
		return $counter;
	}
}

?>